package com.zzyl.nursing.service.impl;

import java.util.Date;
import java.util.List;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.zzyl.common.utils.DateUtils;
import com.zzyl.common.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zzyl.nursing.mapper.AlertDataMapper;
import com.zzyl.nursing.domain.AlertData;
import com.zzyl.nursing.service.IAlertDataService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
/**
 * 报警数据Service业务层处理
 * 
 * @author syl
 * @date 2025-06-05
 */
@Service
public class AlertDataServiceImpl extends ServiceImpl<AlertDataMapper,AlertData> implements IAlertDataService
{
    @Autowired
    private AlertDataMapper alertDataMapper;

//    /**
//     * 查询报警数据
//     *
//     * @param id 报警数据主键
//     * @return 报警数据
//     */
//    @Override
//    public AlertData selectAlertDataById(Long id)
//    {
//        return getById(id);
//    }

//    /**
//     * 查询报警数据列表
//     *
//     * @param alertData 报警数据
//     * @return 报警数据
//     */
//    @Override
//    public List<AlertData> selectAlertDataList(AlertData alertData)
//    {
//        return alertDataMapper.selectAlertDataList(alertData);
//    }

    /**
     * 新增报警数据
     * 
     * @param alertData 报警数据
     * @return 结果
     */
    @Override
    public int insertAlertData(AlertData alertData)
    {
        return save(alertData)?1:0;
    }

    /**
     * 修改报警数据
     * 
     * @param alertData 报警数据
     * @return 结果
     */
    @Override
    public int updateAlertData(AlertData alertData)
    {
        return updateById(alertData)?1:0;
    }

    /**
     * 批量删除报警数据
     * 
     * @param ids 需要删除的报警数据主键
     * @return 结果
     */
    @Override
    public int deleteAlertDataByIds(Long[] ids)
    {
        return removeByIds(Arrays.asList(ids))?1:0;
    }

    /**
     * 删除报警数据信息
     * 
     * @param id 报警数据主键
     * @return 结果
     */
    @Override
    public int deleteAlertDataById(Long id)
    {
        return removeById(id)?1:0;
    }

    /**
     * 根据护理员ID查询报警数据列表
     */
    @Override
    public List<AlertData> selectAlertDataListByNurse(AlertData alertData, Long nurseId) {
        if (alertData.getParams() == null) {
            alertData.setParams(new java.util.HashMap<>());
        }
        alertData.getParams().put("nurseId", nurseId);
        return alertDataMapper.selectAlertDataListByNurse(alertData);
    }

    /**
     * 处理报警数据
     */
    @Override
    @Transactional
    public int processAlert(Long id, String processingResult, Long nurseId, String nurseName) {
        AlertData alertData = alertDataMapper.selectAlertDataById(id);
        if (alertData == null) {
            throw new RuntimeException("报警数据不存在");
        }

        int permissionCount = alertDataMapper.checkNursePermission(alertData.getUserId(), nurseId);
        if (permissionCount == 0) {
            throw new RuntimeException("无权处理该报警数据");
        }

        // 3. 使用 LambdaUpdateWrapper 进行部分字段更新
        boolean result = update(new LambdaUpdateWrapper<AlertData>()
                .eq(AlertData::getId, id)
                .set(AlertData::getProcessingResult, processingResult)
                .set(AlertData::getProcessorId, nurseId)
                .set(AlertData::getProcessorName, nurseName)
                .set(AlertData::getProcessingTime, new Date())
                .set(AlertData::getStatus, 1) // 设置为已处理
                .set(AlertData::getUpdateTime, new Date()));

        return result ? 1 : 0;
    }
    /**
     * 查询报警数据列表
     */
    @Override
    public List<AlertData> selectAlertDataList(AlertData alertData) {
        // 获取当前登录护理员ID
        Long nurseId = SecurityUtils.getUserId();
        return selectAlertDataListByNurse(alertData, nurseId);
    }

    /**
     * 查询报警数据
     */
    @Override
    public AlertData selectAlertDataById(Long id) {
        AlertData alertData = getById(id);
        if (alertData != null) {
            // 验证当前用户是否有权限查看
            Long nurseId = SecurityUtils.getUserId();
            int permissionCount = alertDataMapper.checkNursePermission(alertData.getUserId(), nurseId);
            if (permissionCount == 0) {
                throw new RuntimeException("无权查看该报警数据");
            }
        }
        return alertData;
    }
}
